using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using way.model;
using way.util;

namespace way.util
{
    public static class ResultCodeExtensions
    {
        /**
         * StringAPI动态实现
         * 字典生成枚举类
         */
        private static readonly Dictionary<ResultCode, string> _codeDescMap = new Dictionary<ResultCode, string>
        {
            #region menu
            { ResultCode.menuCreateTableError,"menu create table error." },
            { ResultCode.menuCreateTableSuccess,"menu create table success."},
            { ResultCode.menuUpIdAreaFalse,"menuUpIdAreaFalse."},
            { ResultCode.menuAfterIdAreaFalse,"menuAfterIdAreaFalse."},
            { ResultCode.menuBeforeIdAreaFalse,"menuBeforeIdAreaFalse."},
            { ResultCode.menuHeadNoLast,"menuHeadNoLast."},
            { ResultCode.menuHeadNull,"menuHeadNull."},
            { ResultCode.menuQueryNull,"menuQueryNull."},
            { ResultCode.menuQuerySuccess,"menuQuerySuccess."},
            { ResultCode.menuSortFail,"menuSortFail."},
            { ResultCode.menuSortSuccess,"menuSortSuccess."},
            { ResultCode.menuChangeHasMe,"menuChangeHasMe."},
            { ResultCode.menuIdAreaFalse,"menuIdAreaFalse."},
            { ResultCode.menuUpdateNameById,"menuUpdateNameById."},
            { ResultCode.menuIdNotProvide,"menuIdNotProvide."},
            { ResultCode.menuNodeSelectNull,"menuNodeSelectNull."},
            { ResultCode.menuTreeViewSuccess,"menuTreeViewSuccess."},
            { ResultCode.menuDeleteOneSuccess,"menuDeleteOneSuccess."},
            { ResultCode.menuAddHeadException,"menuAddHeadException."},
            { ResultCode.menuUpNotFound,"menuUpNotFound."},
            { ResultCode.menuBeforeNotFound,"menuBeforeNotFound."},
            { ResultCode.menuChangeNotHasMe,"menuChangeNotHasMe."},
            #endregion
             #region database
            { ResultCode.databaseTableCheckError,"databaseTableCheckError." },
            { ResultCode.databaseInsertFail,"databaseInsertFail." },
            { ResultCode.databaseUpdateFail,"databaseUpdateFail." },
            { ResultCode.databaseQueryIdNull,"databaseQueryIdNull." },
            { ResultCode.databaseInsertAfterQueryIdNull,"databaseInsertAfterQueryIdNull." },
            { ResultCode.databaseDeleteDataNoSelect,"databaseDeleteDataNoSelect." },
            { ResultCode.databaseNameExist,"databaseNameExist." },
            #endregion
             #region unclass
             { ResultCode.nomalStatus,"nomalStatus." },
             { ResultCode.checkFail,"checkFail." },
             { ResultCode.exceptionStatus,"exceptionStatus." },
             { ResultCode.success,"success." },
             { ResultCode.resultNull,"resultNull." },
             { ResultCode.dataNull,"dataNull." },
             { ResultCode.dataSelectNull,"dataSelectNull." },
             { ResultCode.dataNameNull,"dataNameNull." },
             { ResultCode.methodNotFound,"methodNotFound." },
             { ResultCode.saveFailedAppConfig,"saveFailed." },
             #endregion
        };
        public static bool IsSuccess(int code) {
            return code % 2 == 0;
        }
        public static bool IsSuccess(ResultCode code)
        {
            return (int)code % 2 == 0;
        }
        public static bool IsSuccess<T>(Result<T> result)
        {
            return (int)result.status % 2 == 0;
        }

        public static string GetDesc(this ResultCode code) {
            /* string result=string.Empty;
             switch (code) {
                 case ResultCode.menuCreateTableError:
                     result = "菜单表创建失败";
                     break;

             }
             return result;*/
            return _codeDescMap.ContainsKey(code) ? _codeDescMap[code] : "unknown status";
        }
        public static string GetDesc(int code)
        {
            /* string result=string.Empty;
             switch (code) {
                 case ResultCode.menuCreateTableError:
                     result = "菜单表创建失败";
                     break;

             }
             return result;*/
            ResultCode resultCode = (ResultCode)code;
            return _codeDescMap.ContainsKey(resultCode) ? _codeDescMap[resultCode] : "unknown status";
        }
    }
}
